﻿
namespace Biciq.Web.UI.Servicios
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using Biciq.Datos;
    using Biciq.Logica;
    using Biciq.Datos.Clases;


    // Implements application logic using the BiciqContexto context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class SeguridadesDomainService : LinqToEntitiesDomainService<BiciqContexto>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Accesoes' query.
        public IQueryable<Acceso> GetAccesoes()
        {
            return this.ObjectContext.Accesoes;
        }

        public void InsertAcceso(Acceso acceso)
        {
            if ((acceso.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(acceso, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Accesoes.AddObject(acceso);
            }
        }

        public void UpdateAcceso(Acceso currentAcceso)
        {
            this.ObjectContext.Accesoes.AttachAsModified(currentAcceso, this.ChangeSet.GetOriginal(currentAcceso));
        }

        public void DeleteAcceso(Acceso acceso)
        {
            if ((acceso.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(acceso, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Accesoes.Attach(acceso);
                this.ObjectContext.Accesoes.DeleteObject(acceso);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Agencias' query.
        public IQueryable<Agencia> GetAgencias()
        {
            return this.ObjectContext.Agencias;
        }

        public void InsertAgencia(Agencia agencia)
        {
            if ((agencia.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(agencia, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Agencias.AddObject(agencia);
            }
        }

        public void UpdateAgencia(Agencia currentAgencia)
        {
            this.ObjectContext.Agencias.AttachAsModified(currentAgencia, this.ChangeSet.GetOriginal(currentAgencia));
        }

        public void DeleteAgencia(Agencia agencia)
        {
            if ((agencia.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(agencia, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Agencias.Attach(agencia);
                this.ObjectContext.Agencias.DeleteObject(agencia);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Auditorias' query.
        public IQueryable<Auditoria> GetAuditorias()
        {
            return this.ObjectContext.Auditorias;
        }

        public void InsertAuditoria(Auditoria auditoria)
        {
            if ((auditoria.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(auditoria, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Auditorias.AddObject(auditoria);
            }
        }

        public void UpdateAuditoria(Auditoria currentAuditoria)
        {
            this.ObjectContext.Auditorias.AttachAsModified(currentAuditoria, this.ChangeSet.GetOriginal(currentAuditoria));
        }

        public void DeleteAuditoria(Auditoria auditoria)
        {
            if ((auditoria.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(auditoria, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Auditorias.Attach(auditoria);
                this.ObjectContext.Auditorias.DeleteObject(auditoria);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Cargoes' query.
        public IQueryable<Cargo> GetCargoes()
        {
            return this.ObjectContext.Cargoes;
        }

        public void InsertCargo(Cargo cargo)
        {
            if ((cargo.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cargo, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Cargoes.AddObject(cargo);
            }
        }

        public void UpdateCargo(Cargo currentCargo)
        {
            this.ObjectContext.Cargoes.AttachAsModified(currentCargo, this.ChangeSet.GetOriginal(currentCargo));
        }

        public void DeleteCargo(Cargo cargo)
        {
            if ((cargo.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cargo, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Cargoes.Attach(cargo);
                this.ObjectContext.Cargoes.DeleteObject(cargo);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Clientes' query.
        public IQueryable<Cliente> GetClientes()
        {
            return this.ObjectContext.Clientes;
        }

        public void InsertCliente(Cliente cliente)
        {
            if ((cliente.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cliente, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Clientes.AddObject(cliente);
            }
        }

        public void UpdateCliente(Cliente currentCliente)
        {
            this.ObjectContext.Clientes.AttachAsModified(currentCliente, this.ChangeSet.GetOriginal(currentCliente));
        }

        public void DeleteCliente(Cliente cliente)
        {
            if ((cliente.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cliente, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Clientes.Attach(cliente);
                this.ObjectContext.Clientes.DeleteObject(cliente);
            }
        }

       

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Empleadoes' query.
        public IQueryable<Empleado> GetEmpleadoes()
        {
            return this.ObjectContext.Empleadoes;
        }

        public void InsertEmpleado(Empleado empleado)
        {
            if ((empleado.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(empleado, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Empleadoes.AddObject(empleado);
            }
        }

        public void UpdateEmpleado(Empleado currentEmpleado)
        {
            this.ObjectContext.Empleadoes.AttachAsModified(currentEmpleado, this.ChangeSet.GetOriginal(currentEmpleado));
        }

        public void DeleteEmpleado(Empleado empleado)
        {
            if ((empleado.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(empleado, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Empleadoes.Attach(empleado);
                this.ObjectContext.Empleadoes.DeleteObject(empleado);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Pais' query.
        public IQueryable<Pai> GetPais()
        {
            return this.ObjectContext.Pais;
        }

        public void InsertPai(Pai pai)
        {
            if ((pai.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(pai, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Pais.AddObject(pai);
            }
        }

        public void UpdatePai(Pai currentPai)
        {
            this.ObjectContext.Pais.AttachAsModified(currentPai, this.ChangeSet.GetOriginal(currentPai));
        }

        public void DeletePai(Pai pai)
        {
            if ((pai.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(pai, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Pais.Attach(pai);
                this.ObjectContext.Pais.DeleteObject(pai);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Parametroes' query.
        public IQueryable<Parametro> GetParametroes()
        {
            return this.ObjectContext.Parametroes;
        }

        public void InsertParametro(Parametro parametro)
        {
            if ((parametro.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(parametro, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Parametroes.AddObject(parametro);
            }
        }

        public void UpdateParametro(Parametro currentParametro)
        {
            this.ObjectContext.Parametroes.AttachAsModified(currentParametro, this.ChangeSet.GetOriginal(currentParametro));
        }

        public void DeleteParametro(Parametro parametro)
        {
            if ((parametro.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(parametro, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Parametroes.Attach(parametro);
                this.ObjectContext.Parametroes.DeleteObject(parametro);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Recursoes' query.
        public IQueryable<Recurso> GetRecursoes()
        {
            return this.ObjectContext.Recursoes;
        }

        public void InsertRecurso(Recurso recurso)
        {
            if ((recurso.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(recurso, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Recursoes.AddObject(recurso);
            }
        }

        public void UpdateRecurso(Recurso currentRecurso)
        {
            this.ObjectContext.Recursoes.AttachAsModified(currentRecurso, this.ChangeSet.GetOriginal(currentRecurso));
        }

        public void DeleteRecurso(Recurso recurso)
        {
            if ((recurso.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(recurso, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Recursoes.Attach(recurso);
                this.ObjectContext.Recursoes.DeleteObject(recurso);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Rols' query.
        public IQueryable<Rol> GetRols()
        {
            return this.ObjectContext.Rols;
        }

        public void InsertRol(Rol rol)
        {
            if ((rol.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(rol, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Rols.AddObject(rol);
            }
        }

        public void UpdateRol(Rol currentRol)
        {
            this.ObjectContext.Rols.AttachAsModified(currentRol, this.ChangeSet.GetOriginal(currentRol));
        }

        public void DeleteRol(Rol rol)
        {
            if ((rol.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(rol, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Rols.Attach(rol);
                this.ObjectContext.Rols.DeleteObject(rol);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'sysdiagrams' query.
        public IQueryable<sysdiagram> GetSysdiagrams()
        {
            return this.ObjectContext.sysdiagrams;
        }

        public void InsertSysdiagram(sysdiagram sysdiagram)
        {
            if ((sysdiagram.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(sysdiagram, EntityState.Added);
            }
            else
            {
                this.ObjectContext.sysdiagrams.AddObject(sysdiagram);
            }
        }

        public void UpdateSysdiagram(sysdiagram currentsysdiagram)
        {
            this.ObjectContext.sysdiagrams.AttachAsModified(currentsysdiagram, this.ChangeSet.GetOriginal(currentsysdiagram));
        }

        public void DeleteSysdiagram(sysdiagram sysdiagram)
        {
            if ((sysdiagram.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(sysdiagram, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.sysdiagrams.Attach(sysdiagram);
                this.ObjectContext.sysdiagrams.DeleteObject(sysdiagram);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Tarjetas' query.
        public IQueryable<Tarjeta> GetTarjetas()
        {
            return this.ObjectContext.Tarjetas;
        }

        public void InsertTarjeta(Tarjeta tarjeta)
        {
            if ((tarjeta.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tarjeta, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Tarjetas.AddObject(tarjeta);
            }
        }

        public void UpdateTarjeta(Tarjeta currentTarjeta)
        {
            this.ObjectContext.Tarjetas.AttachAsModified(currentTarjeta, this.ChangeSet.GetOriginal(currentTarjeta));
        }

        public void DeleteTarjeta(Tarjeta tarjeta)
        {
            if ((tarjeta.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tarjeta, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Tarjetas.Attach(tarjeta);
                this.ObjectContext.Tarjetas.DeleteObject(tarjeta);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TipoSuscripcions' query.
        public IQueryable<TipoSuscripcion> GetTipoSuscripcions()
        {
            return this.ObjectContext.TipoSuscripcions;
        }

        public void InsertTipoSuscripcion(TipoSuscripcion tipoSuscripcion)
        {
            if ((tipoSuscripcion.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tipoSuscripcion, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TipoSuscripcions.AddObject(tipoSuscripcion);
            }
        }

        public void UpdateTipoSuscripcion(TipoSuscripcion currentTipoSuscripcion)
        {
            this.ObjectContext.TipoSuscripcions.AttachAsModified(currentTipoSuscripcion, this.ChangeSet.GetOriginal(currentTipoSuscripcion));
        }

        public void DeleteTipoSuscripcion(TipoSuscripcion tipoSuscripcion)
        {
            if ((tipoSuscripcion.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tipoSuscripcion, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TipoSuscripcions.Attach(tipoSuscripcion);
                this.ObjectContext.TipoSuscripcions.DeleteObject(tipoSuscripcion);
            }
        }
                // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Usuarios' query.
        public IQueryable<Usuario> GetUsuarios()
        {
            return this.ObjectContext.Usuarios;
        }

        public void InsertUsuario(Usuario usuario)
        {
            if ((usuario.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(usuario, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Usuarios.AddObject(usuario);
            }
        }

        public void UpdateUsuario(Usuario currentUsuario)
        {
            this.ObjectContext.Usuarios.AttachAsModified(currentUsuario, this.ChangeSet.GetOriginal(currentUsuario));
        }

        public void DeleteUsuario(Usuario usuario)
        {
            if ((usuario.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(usuario, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Usuarios.Attach(usuario);
                this.ObjectContext.Usuarios.DeleteObject(usuario);
            }
        }

        public Result ValidarUsuario(DatosLogin datosLogin)
        {
            SeguridadServicio seguridades = new SeguridadServicio();
            return seguridades.ValidarUsuario(datosLogin.Usuario, datosLogin.Clave);
        }

        public Usuario ObtenerUsuarioPorNombre(string nombreUsuario)
        {
            
            return this.ObjectContext.Usuarios
                .Include("Rol")
                .Include("Rol.Accesoes")
                .Include("Rol.Accesoes.Recurso")
                .Include("Empleadoes")
                .Where(u => u.estado == "ACT" && u.nombreUsuario == nombreUsuario).FirstOrDefault();

        }

       
    }
}


